import { clear_gun, get_gun } from 'base/api';
import { s_psn, g_db } from 'db/state';
import { do_event } from './event';
import { obj_str, cline, uReal } from 'sui';
import { put_log } from 'base/ctl/log';

const do_ckpsn = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描轴承编码");

    clear_gun();
    s_psn(psn);

    umd.set("psn", psn);

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    umd.setErr("");

    uReal(null, "pair/info",
        {
            line: cline(),
            k: psn,
        },
        r => {
            if (r.tech) {
                const tech_sn = obj_str(r.tech, "sn");

                g_db.s_tech(tech_sn);

                umd.mset({
                    "tech": tech_sn,
                });
            }

            if (r.task) {
                const task_sn = obj_str(r.task, "sn");

                g_db.s_task(task_sn);
                umd.set("task", task_sn);
            }
        });

    do_event("step_over", umd, watch);
};

const do_hand = (stepnum, step, umd, watch) => {
    g_db.stop_scan();

    umd.openHand(true);

    watch.startWatch();
};

export {do_ckpsn, do_hand};
